﻿/*
 * Copyright © 2011 Tristan Reeves, treeves@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System.Collections.Generic;
using Spring.Objects.Factory;

namespace Sprinkles.Tests.DeploymentHosts
{
    public class PrototypeHost : DeploymentHost
    {
        protected override bool Include
        {
            get { return this.UserContextIsNotMarked(); }
        }

        public static int POST_CTR = 0;
        public static int PRE_CTR = 0;

        public static List<string> PREINIT_MESSAGES = new List<string>();
        public static List<string> POSTINIT_MESSAGES = new List<string>();

        public static int TEXT_CTR = 0;
        public static int HELLO_CTR = 0;

        public class PrototypeObject
        {
            public string Text = "hola." + (++TEXT_CTR);
            public string Pretext;
            public string Posttext;
        };
        public class Object1
        {
            public string Name;
            public Object1()
            {
                HELLO_CTR++;
                Name = "hello." + HELLO_CTR;
            }
        }
        public class Object2
        {
            public Object1 PreLink;
            public Object1 PostLink;
        }

        public static bool DO_INCLUDE = false;
        public static string Qualify(string name)
        {
            return "PrototypeHost." + name;
        }

        protected override void RegisterDeployments()
        {
            base.Register<PrototypeObject>()
                .Name(Qualify("obj"))
                .Singleton(false)

                .PreInitCallback(x => PrototypeHost.PRE_CTR--)
                .PreInitCallback(x => PREINIT_MESSAGES.Add("pre: " + x.Text))
                .PreInitCallback(x => x.Pretext = "pre: " + x.Text)

                .PostInitCallback(x => PrototypeHost.POST_CTR++)
                .PostInitCallback(x => POSTINIT_MESSAGES.Add("post: " + x.Text))
                .PostInitCallback(x => x.Posttext = "post: " + x.Text)
                .Done();

            if (DO_INCLUDE)
            {
                base.Register<Object1>()
                    .Name(Qualify("object1"))
                    .Singleton(false)
                    .Done();

                base.Register<Object2>()
                    .Name(Qualify("object2"))
                    .PreInitCallback(x => x.PreLink = ((IObjectFactory)ContainerContext)[Qualify("object1")] as Object1)
                    .PostInitCallback(x => x.PostLink = ((IObjectFactory)ContainerContext)[Qualify("object1")] as Object1)
                    .Done();
            }
        }
    }
}
